package com.kc.sjwh.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.po.Role;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.RoleService;

/**
 * 用户管理
 * @author zhangguihua(hua7381@163.com)
 * date: 2017年8月4日
 */
@Controller
@RequestMapping("role")
public class RoleAction extends BaseAction {
	static Logger logger = Logger.getLogger(RoleAction.class);

	@Autowired
	RoleService service;
	
	//打开列表页面
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "role/roleList";
	}

	//获取分页
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Page<Role> page = service.getPage(pageSize, pageNum, super.getParamMap(req));
		return JSON.toJSONString(page);
	}

	//打开表单
	@RequestMapping("form")
	public String form(HttpServletRequest req, String act, String id) {
		req.setAttribute("act", act);
		req.setAttribute("id", id);
		req.setAttribute("actName", "add".equals(act)?"新增":"修改");
		return "role/roleForm_"+act;
	}

	//表单加载
	@ResponseBody
	@RequestMapping("load4Update")
	public String load4Update(HttpServletRequest req, String id) throws Exception {
		Map m = service.load4Update(id);
		return JSON.toJSONString(m);
	}

	//新增
	@ResponseBody
	@RequestMapping("add")
	public String add(HttpServletRequest req, Role one) throws Exception {
		one.setCjrxm(getLoginUser().getMc());
		service.add(one);
		return JSON.toJSONString(one.getId());
	}

	//修改
	@ResponseBody
	@RequestMapping("update")
	public String update(HttpServletRequest req, Role one, String zyids) throws Exception {
		service.update(one, zyids);
		return JSON.toJSONString(null);
	}

	//删除
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(null);
	}
}
